/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnCouShop;
import com.ykht.proj.coupon.entity.CnFootPrint;
import com.ykht.proj.coupon.mapper.CnCouShopMapper;
import com.ykht.proj.coupon.mapper.CnFootPrintMapper;
import com.ykht.proj.coupon.service.CnBrandService;
import com.ykht.proj.coupon.service.CnFootPrintService;
import com.ykht.proj.coupon.service.CnShopService;
import com.ykht.proj.coupon.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 足迹表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@Service
public class CnFootPrintServiceImpl extends ServiceImpl<CnFootPrintMapper, CnFootPrint> implements CnFootPrintService {

	@Resource
	private CnFootPrintMapper cnFootPrintMapper;
	@Resource
	private CnBrandService cnBrandService;
	@Resource
	private CnShopService cnShopService;
	@Resource
	private  CnCouShopMapper cnCouShopMapper;
	@Override
	public IPage<CnFootPrintPageResp> opPage(Page page, CnFootPrintPageReq req) {
		Long loginId = SecurityUtils.getPersonId();

		CnFootPrint cnFootPrint = new CnFootPrint();
		cnFootPrint.setPerId(loginId);
		cnFootPrint.setType(req.getType());
		IPage<CnFootPrintPageResp> ipage = cnFootPrintMapper.opPage(page,cnFootPrint);

		ipage.getRecords().parallelStream().collect(Collectors.toList()).forEach(o->{
			if (0==o.getType()){//品牌
				PinPaiGuanVo pinPaiGuanVo = cnBrandService.pinPaiGuan(o.getTatId());
				o.setBrand(pinPaiGuanVo);
			}else if (1==o.getType()){//门店

				AboutShopReq aboutShopReq = new AboutShopReq();
				BeanUtil.copyProperties(req,aboutShopReq);
				aboutShopReq.setId(o.getTatId());
				aboutShopReq.setPerId(loginId);
				AboutShopVo aboutShopVo = cnShopService.aboutShop(aboutShopReq);
				List temp = cnCouShopMapper.selectList(Wrappers.lambdaQuery(CnCouShop.class).eq(CnCouShop::getShopId,aboutShopReq.getId()));
				aboutShopVo.setCouponCount(temp.size()+aboutShopVo.getTyCouponCount());
				o.setShop(aboutShopVo);
			}
		});

		return ipage;
	}
}
